// 发送邮件
// import { createContext } from 'node:vm';
import nodemailer from 'nodemailer';
import { Models } from '../typings/model';

// 开启一个 SMTP 连接池
let transporter = nodemailer.createTransport({
    service: 'qq',
    port: 456,
    secure: true, // secure:true for port 465, secure:false for port 587
    auth: {
        user: '1121145488@qq.com',
        pass: 'qzoviallfzrbjidh' // QQ邮箱需要使用授权码
    },
    // secureConnection: true, // use SSL
});

// 设置邮件内容（谁发送什么给谁）
let mailOptions: Models.MailOptions = {
    from: '', // 发件人
    to: '', // 收件人
    subject: '', // 主题
    text: '', // plain text body
    html: '', // html body
};

async function sedEmail({from= '"聂万君" <1121145488@qq.com>',to,subject,text,html}:Models.MailOptions) {
    mailOptions.from = from
    mailOptions.to = to
    mailOptions.subject = subject
    mailOptions.text = text
    mailOptions.html = html
    try {
        let res = await transporter.sendMail(mailOptions);
        if(res.response.indexOf('250') > -1) {
            return {
                data: res.response,
                success: true
            }
        }
        else {
            console.error(res)
            return {
                success: false
            }
    }
    } catch (error) {
        return {
            success: false,
            data: error
        }
    }
    
}

export default sedEmail